package com.example.back.controller;

import com.example.back.entity.Like;
import com.example.back.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
public class LikeController {
    @Autowired
    private LikeService likeService;
    @PostMapping("/like")
    public int addLike(@RequestBody Like like){
        return likeService.addLike(like);
    }
    @DeleteMapping("/like/{postId}/{userId}")  // 路径包含postId和userId
    public int deleteLike(@PathVariable Integer postId, @PathVariable Integer userId) {
        return likeService.deleteLike(postId, userId);
    }
    @PutMapping("/like")
    public int updateLike(@RequestBody Like like){
        return likeService.updateLike(like);
    }
    @GetMapping("/like")
    public ArrayList<Like> queryLike( Like like){
        return likeService.queryLike(like);
    }


    // 查询用户是否已点赞该帖子
    @GetMapping("/like/{postId}/{userId}")
    public Like getLikeByUserAndPost(@PathVariable Integer postId,@PathVariable Integer userId){
        return likeService.getLikeByUserAndPost(postId,userId);
    }

    // 查询用户点赞数【注意路径】
    @GetMapping("/likes/{postId}")
    public int countLikeByPostId(@PathVariable Integer postId){
        return likeService.countLikeByPostId(postId);
    }
    @GetMapping("/like/user/{userId}")
    public int countUserLikes(@PathVariable Integer userId){
        return likeService.countUserLikes(userId);
    }
}
